package com.dgshanger.sanhxiaofeisc.ui.home.activity

import android.annotation.TargetApi
import android.content.pm.ActivityInfo
import android.os.Build
import android.util.Log
import android.view.View
import android.view.WindowManager
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.dgshanger.sanhxiaofeisc.ui.home.utils.SwitchVideoModel
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import com.xx.baseuilibrary.mvp.BaseMvpViewActivity
import kotlinx.android.synthetic.main.activity_home_video2.*
import java.util.*

/**
 * Created by JacobHHH on 2018/5/14.
 * 首页视频
 */
class HomeVideo2Activity : BaseMvpViewActivity() {

    private lateinit var mVideoUrl : String
    private lateinit var mOrientationUtils : OrientationUtils

    override fun getActivityLayoutId(): Int = R.layout.activity_home_video2

    override fun initData() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        mVideoUrl= ImageLoader.getUrl(intent.getStringExtra("videoUrl"))
        Log.e("Tag","mVideoUrl="+mVideoUrl)
        val title = intent.getStringExtra("title")
        //tv_video_title.text=title
        val source1 = mVideoUrl
        val name = getString(R.string.common)
        val switchVideoModel = SwitchVideoModel(name, source1)

        val source2 = mVideoUrl
        val name2 = getString(R.string.distinct)
        val switchVideoModel2 = SwitchVideoModel(name2, source2)

        val list = ArrayList<SwitchVideoModel>()
        list.add(switchVideoModel)
        list.add(switchVideoModel2)

        video_player.setUp(list, true, title)

        //增加封面
        /*val imageView = ImageView(this)
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        imageView.setImageResource(R.drawable.bangfang_)
        video_player.setThumbImageView(imageView)*/

        //增加title
        video_player.getTitleTextView().setVisibility(View.VISIBLE)

        //设置返回键
        video_player.getBackButton().setVisibility(View.VISIBLE)

        //设置旋转
        mOrientationUtils = OrientationUtils(this, video_player)

        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏
        video_player.getFullscreenButton().setOnClickListener{ mOrientationUtils.resolveByClick() }

        //是否可以滑动调整
        video_player.setIsTouchWiget(true)

        //设置返回按键功能
        video_player.getBackButton().setOnClickListener{ onBackPressed() }
    }

    override fun initEvent() {

    }

    override fun onPause() {
        super.onPause()
        video_player.onVideoPause()
    }

    override fun onResume() {
        super.onResume()
        video_player.onVideoResume()
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    override fun onDestroy() {
        super.onDestroy()
        if (mOrientationUtils != null)
            mOrientationUtils.releaseListener()
    }

    override fun onBackPressed() {
        //先返回正常状态
        if (mOrientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            video_player.getFullscreenButton().performClick()
            return
        }
        //释放所有
        video_player.setVideoAllCallBack(null)
        GSYVideoManager.releaseAllVideos()
        finish()

    }
}